/**
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

//SMUIML v0.4

package diuf.diva.hephaistk.xml.smuiml;

import diuf.diva.hephaistk.xml.Attribute;
import diuf.diva.hephaistk.xml.HephaisTKParsingException;
import diuf.diva.hephaistk.xml.XmlGeneric;

public class Variable extends Smuiml_Generic {
	/**
	 *
	 */
	private static final long serialVersionUID = -2661669871405478079L;

	public final static String NAME = "variable";

	private Attribute attName = null;
	private Attribute attValue = null;
	private Attribute attType = null;

	public Variable(){
		super();
		setName(NAME);
	}

	/**
	 * @return the attName
	 */
	public Attribute getAttName() {
		return attName;
	}
	/**
	 * @param attName the attName to set
	 */
	public void setAttName(String attName) {
		this.attName = new Attribute("name", attName);
		attributes.add(this.attName);
	}
	/**
	 * @return the attType
	 */
	public Attribute getAttType() {
		return attType;
	}
	/**
	 * @param attType the attType to set
	 */
	public void setAttType(String attType) {
		this.attType = new Attribute("type", attType);
		attributes.add(this.attType);
	}
	/**
	 * @return the attValue
	 */
	public Attribute getAttValue() {
		return attValue;
	}
	/**
	 * @param attValue the attValue to set
	 */
	public void setAttValue(String attValue) {
		this.attValue = new Attribute("value", attValue);
		attributes.add(this.attValue);
	}

	public void addElement(XmlGeneric element) throws HephaisTKParsingException {
		throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected element: "+element.getName());
	}

	@Override
	public void addAttribute(Attribute attribute) throws HephaisTKParsingException{
		super.addAttribute(attribute);
		String attName = attribute.getName();
		if (attName.indexOf(':') != -1) {
			attName = attName.substring(attName.indexOf(':')+1);
		}
		addAttribute(attName, attribute.getValue());
	}

	public void addAttribute(String name, String value) throws HephaisTKParsingException{
		if(name.equalsIgnoreCase("name")){
			setAttName(value);
		}else if(name.equalsIgnoreCase("value")){
			setAttValue(value);
		}else if(name.equalsIgnoreCase("type")){
			setAttType(value);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected attribute: "+name);
		}
	}

	public String toString(int indent) {
		StringBuilder ret = new StringBuilder();
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("<"+NAME+(attName != null? " name=\""+attName.getValue()+"\"" : "")+
				(attValue != null? " value=\""+attValue.getValue()+"\"" : "")+
				(attType != null? " type=\""+attType.getValue()+"\"" : "")+"/>\n");
		return ret.toString();
	}
}
